package com.atguigu.servicebase.exceptionhandler;

import com.atguigu.commonutils.ExceptionUtil;
import com.atguigu.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * User:曹帅
 * Date:2021/2/5
 * Version:1.0
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    //指定出现了什么异常，会执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody   //返回json
    public R error(Exception e) {
        e.printStackTrace();
        return R.error().message("全局异常处理");
    }

    //特定异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody   //返回json
    public R error(ArithmeticException e) {
        e.printStackTrace();
        return R.error().message("ArithmeticException异常");
    }

    //特定异常处理
    @ExceptionHandler(GuliException.class)
    @ResponseBody   //返回json
    public R error(GuliException e) {
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return R.error().code(e.getCode()).message(e.getMsg());
    }

}
